package com.apollow.oschina.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apollow.oschina.R;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.BlogBean;
import com.apollow.oschina.protocol.UserBlogProtocol;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Dragon on 2017/1/13.
 */

public class BlogActivity extends AppCompatActivity {

    @BindView(R.id.rv_blog)
    RecyclerView mRvBlog;
    private TextView mTvTitle;
    private UserBlogProtocol mProtocol;
    private List<BlogBean> mBlogs;
    private BlogAdapter mAdapter;
    private LinearLayout mLlBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog);
        ButterKnife.bind(this);
        initData();
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mLlBack = (LinearLayout) findViewById(R.id.ll_back);
        mTvTitle.setText("用户博客列表");
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRvBlog.setLayoutManager(manager);
        mAdapter = new BlogAdapter();
        mRvBlog.setAdapter(mAdapter);
        initEvent();
    }


    private void initEvent() {
        mLlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        mProtocol = new UserBlogProtocol();
        new Thread(new Runnable() {
            @Override
            public void run() {
                mBlogs = mProtocol.getData().getBlogs();
                Global.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }

    public class BlogAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = Global.inflate(R.layout.item_activity_blog, parent);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.setData(mBlogs.get(position));
        }

        @Override
        public int getItemCount() {
            return mBlogs == null ? 0 : mBlogs.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.tv_title)
            TextView mTvTitle;
            @BindView(R.id.author)
            TextView mAuthor;
            @BindView(R.id.tv_time)
            TextView mTvTime;
            public TextView mTvCount;

            //            @BindView(R.id.tv_count)
//            TextView mTvCount;
            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                mTvCount = (TextView) itemView.findViewById(R.id.tv_count);
            }

            public void setData(BlogBean blogBean) {
                mTvTitle.setText(blogBean.getTitle());
                mTvCount.setText(blogBean.getCommentCount() + "");
                mAuthor.setText(blogBean.getAuthorname());
                mTvTime.setText(blogBean.getPubDate());
            }
        }
    }
}
